/*
 * Copyright (c) 2012 Shanda Corporation. All rights reserved.
 *
 * Created on 2012-8-17.
 */

package com.whq.knowverywell.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.whq.knowverywell.R;
import com.whq.knowverywell.business.LoginUtil;
import com.whq.knowverywell.util.LogUtil;

/**
 * acivity基类.
 *
 * @author wanghuaiqiang
 */
public class BaseActivity extends SherlockFragmentActivity implements OnClickListener, OnKeyListener {
    private final static String TAG = "BaseActivity";

    protected ProgressDialog mLoadProgressDialog = null;
    
    protected Animation mShakeAnimation;
    
    // 屏幕分辨率容器
    protected DisplayMetrics mDisplayMetrics = new DisplayMetrics();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LogUtil.d(TAG, "onCreate start ...");
        super.onCreate(savedInstanceState);
        
        getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
        
        mShakeAnimation = AnimationUtils.loadAnimation(this, R.anim.shake);
        mLoadProgressDialog = new ProgressDialog(this);
        mLoadProgressDialog.setMessage("数据加载中...");
    }

    @Override
    public void onClick(View v) {
        LogUtil.d(TAG, "onClick start ...");
        switch (v.getId()) {
        default:
            break;
        }
    }
    
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        LogUtil.d(TAG, "onKey start ...");
        return false;
    }

    // -------- customize method ---------
    
    /**
     * 打开加载框
     */
    protected void openProgressDialog() {
    	if (mLoadProgressDialog != null && !mLoadProgressDialog.isShowing()) {
    		mLoadProgressDialog.show();
    	}
    }
    
    /**
     * 打开加载框
     * @param message
     */
    protected void openProgressDialog(String message) {
    	if (mLoadProgressDialog != null && !mLoadProgressDialog.isShowing()) {
    		mLoadProgressDialog.setMessage(message);
    		mLoadProgressDialog.show();
    	}
    }
    
    /**
     * 关闭加载框
     */
    protected void closeProgressDialog() {
    	if (mLoadProgressDialog != null && mLoadProgressDialog.isShowing()) {
    		mLoadProgressDialog.dismiss();
    	}
    }
    
    protected void logout() {
    	LoginUtil.clearLogin(this);
    	Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
		this.finish();
    }

}
